﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Hyper.ComponentModel;

namespace BrainTechLLC
{
	[TypeDescriptionProvider(typeof(HyperTypeDescriptionProvider)), Serializable]
	public class StoredParametersAndReturnValue
	{
		public List<int> ListOfParameterHash = new List<int>();
		public List<object> Parameters { get; set; }
		public object ReturnValue { get; set; }

		private byte[] _serializedParams { get; set; }
		private byte[] _serializedRetValue { get; set; }

		public bool IsPacked { get; set; }

		public Exception UnpackageMockFromStorage(bool releaseParameterBytes, bool releaseReturnValueBytes)
		{
			if (!IsPacked)
				return null;

			try
			{
				Parameters = _serializedParams.DeserializeFromCompressedBinary() as List<object>;
				ReturnValue = (_serializedRetValue == null) ? null : (_serializedRetValue.DeserializeFromCompressedBinary() as object);

				if (releaseParameterBytes) _serializedParams = null;
				if (releaseReturnValueBytes) _serializedRetValue = null;

				IsPacked = false;
			}
			catch (Exception ex)
			{
				return ex;
			}

			return null;
		}

		public Exception PackageMockForStorage(bool releaseParameterReference, bool releaseReturnValueReference)
		{
			if (IsPacked)
				return null;

			try
			{
				_serializedParams = Parameters.SerializeToCompressedBinary();
				_serializedRetValue = (ReturnValue == null) ? null : ReturnValue.SerializeToCompressedBinary();

				if (releaseParameterReference) Parameters.Clear();
				if (releaseReturnValueReference) ReturnValue = null;

				IsPacked = true;
			}
			catch (Exception ex) { return ex; }

			return null;
		}
	}
}

